package com.cdxy.model.dto.base.redis.service;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/**
 * @Author: tangy
 * @Date: 2020/2/23 14:55
 */
@Service
@Slf4j
public class RedisService {

    private RedisTemplate redisTemplate;

    private JedisPool jedisPool;


    public RedisService(RedisTemplate redisTemplate, JedisPool jedisPool) {
        this.redisTemplate = redisTemplate;
        this.jedisPool = jedisPool;
    }


    public Object get(String key) {
        if (StringUtils.isBlank(key)) {
            log.error("参数异常，获取失败");
            return null;
        }
        Jedis jedis = null;
        Object result = null;
        try {
            jedis = jedisPool.getResource();
            result = this.redisTemplate.getValueSerializer().deserialize(jedis.get(this.redisTemplate.getKeySerializer().serialize(key)));
        } catch (Exception e) {
            log.error("获取缓存失败", e);
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
        return result;
    }

    public void set(String key, Object value) {
        if (StringUtils.isBlank(key) || value == null) {
            log.error("参数异常，添加缓存失败");
            return;
        }
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.set(this.redisTemplate.getKeySerializer().serialize(key), this.redisTemplate.getValueSerializer().serialize(value));
        } catch (Exception e) {
            log.error("添加缓存失败", e);
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
    }

    public void setEX(String key, Object value, int expirationSecond) {
        if (StringUtils.isBlank(key) || value == null) {
            log.error("参数异常，添加缓存失败");
            return;
        }
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.setex(this.redisTemplate.getKeySerializer().serialize(key), expirationSecond, this.redisTemplate.getValueSerializer().serialize(value));
        } catch (Exception e) {
            log.error("添加缓存失败", e);
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
    }

    public String setNX(String key, Object value, int expirationSecond) {
        String result = "ERROR";
        if (StringUtils.isBlank(key) || value == null) {
            log.error("参数异常，添加缓存失败");
            return result;
        }
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            result = jedis.set(this.redisTemplate.getKeySerializer().serialize(key), this.redisTemplate.getValueSerializer().serialize(value), "NX".getBytes(), "EX".getBytes(), expirationSecond);
        } catch (Exception e) {
            log.error("set Nx 失败", e);
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
        return result;
    }

    public Long queryTtl(String key) {
        if (StringUtils.isBlank(key)) {
            log.error("参数异常");
            return null;
        }
        Long result = null;
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            result = jedis.ttl(this.redisTemplate.getKeySerializer().serialize(key));
        } catch (Exception e) {
            log.error("获取缓存过期时间失败", e);
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
        return result;
    }

    public void setTtl(String key, int expirationSecond) {
        if (StringUtils.isBlank(key)) {
            log.error("参数异常");
            return;
        }
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.expire(this.redisTemplate.getKeySerializer().serialize(key), expirationSecond);
        } catch (Exception e) {
            log.error("设置过期时间失败", e);
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
    }

    public void del(String key) {
        if (StringUtils.isBlank(key)) {
            log.error("参数异常");
            return;
        }
        Jedis jedis = null;
        try {
            jedis = jedisPool.getResource();
            jedis.del(this.redisTemplate.getKeySerializer().serialize(key));
        } catch (Exception e) {
            log.error("删除缓存失败", e);
        } finally {
            if (jedis != null) {
                jedis.close();
            }
        }
    }

}
